package com.shrct.businessprocess.customerservice.impl;

import com.google.common.base.Objects;
import com.shrct.businessprocess.customerservice.RCProductImportService;
import com.shrct.businessprocess.customerservice.RCProjectMock;
import com.shrct.businessprocess.customerservice.RCServiceContractDelegateDAO;
import com.shrct.businessprocess.organization.RCEnterprise;
import com.shrct.businessprocess.organization.RCOrganizationService;
import com.shrct.businessprocess.organization.RCSupplier;
import com.shrct.businessprocess.organization.impl.RCEnterpriseImpl;
import com.shrct.businessprocess.organization.impl.RCSupplierImpl;
import com.shrct.businessprocess.product.RCBrand;
import com.shrct.businessprocess.product.RCDerivedFullServiceProductSpecification;
import com.shrct.businessprocess.product.RCFullServiceProductEntry;
import com.shrct.businessprocess.product.RCIdentifiablePhysicalProductEntry;
import com.shrct.businessprocess.product.RCIdentifiablePhysicalProductEntryDAO;
import com.shrct.businessprocess.product.RCLogicProduct;
import com.shrct.businessprocess.product.RCProductMetaType;
import com.shrct.businessprocess.product.RCProductSpecification;
import com.shrct.businessprocess.product.RCProductSpecificationDAO;
import com.shrct.businessprocess.product.RCServiceLevel;
import com.shrct.businessprocess.product.RCServiceResponseTime;
import com.shrct.businessprocess.product.impl.RCBrandImpl;
import com.shrct.businessprocess.product.impl.RCServiceLevelImpl;
import com.shrct.businessprocess.product.impl.RCServiceResponseTimeImpl;
import edu.fudan.mylang.pf.BaseService;
import edu.fudan.mylang.pf.IGenericQuery;
import edu.fudan.mylang.utils.DateUtil;
import java.util.Date;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.eclipse.xtext.xbase.lib.StringExtensions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 新建／查找客户
 * 新建／查找供应商
 * 新建／查找品牌
 * 新建／查找合同
 * 新建逻辑设备
 * 新建厂商服务
 * 新建承诺服务
 */
@Service("com.shrct.businessprocess.customerservice.RCProductImportService")
public class RCProductImportServiceImpl extends BaseService implements RCProductImportService {
  @Autowired
  private RCOrganizationService organizationService;
  
  @Autowired
  private RCIdentifiablePhysicalProductEntryDAO productService;
  
  @Autowired
  private RCServiceContractDelegateDAO serviceContractDelegateDAO;
  
  @Autowired
  private RCProductSpecificationDAO productSpecificationDAO;
  
  public RCServiceLevel lookupServiceLevel(final String name) {
    RCServiceLevel _xblockexpression = null;
    {
      boolean _isNullOrEmpty = StringExtensions.isNullOrEmpty(name);
      if (_isNullOrEmpty) {
        return null;
      }
      IGenericQuery<RCServiceLevel> query=getObjectFactory().createGenericQuery(RCServiceLevel.class,"select e from com.shrct.businessprocess.product.impl.RCServiceLevelImpl as e   where ((e.name) = (:name)) and  e.active = 1      ").setParameter("name",name);
      RCServiceLevel result = query.uniqueResult();
      boolean _equals = Objects.equal(result, null);
      if (_equals) {RCServiceLevel rCServiceLevel=RCServiceLevelImpl.create(getObjectFactory());
        final Procedure1<RCServiceLevel> _function = new Procedure1<RCServiceLevel>() {
            public void apply(final RCServiceLevel it) {
              Long _id = it.getId();
              String _plus = ("_" + _id);
              it.setCodeid(_plus);
              it.setName(name);
            }
          };
        RCServiceLevel _doubleArrow = ObjectExtensions.<RCServiceLevel>operator_doubleArrow(rCServiceLevel, _function);
        result = _doubleArrow;
      }
      _xblockexpression = (result);
    }
    return _xblockexpression;
  }
  
  public RCServiceResponseTime lookupServiceResponseTime(final String name) {
    RCServiceResponseTime _xblockexpression = null;
    {
      boolean _isNullOrEmpty = StringExtensions.isNullOrEmpty(name);
      if (_isNullOrEmpty) {
        return null;
      }
      IGenericQuery<RCServiceResponseTime> query=getObjectFactory().createGenericQuery(RCServiceResponseTime.class,"select e from com.shrct.businessprocess.product.impl.RCServiceResponseTimeImpl as e   where ((e.name) = (:name)) and  e.active = 1      ").setParameter("name",name);
      RCServiceResponseTime result = query.uniqueResult();
      boolean _equals = Objects.equal(result, null);
      if (_equals) {RCServiceResponseTime rCServiceResponseTime=RCServiceResponseTimeImpl.create(getObjectFactory());
        final Procedure1<RCServiceResponseTime> _function = new Procedure1<RCServiceResponseTime>() {
            public void apply(final RCServiceResponseTime it) {
              Long _id = it.getId();
              String _plus = ("_" + _id);
              it.setCodeid(_plus);
              it.setName(name);
            }
          };
        RCServiceResponseTime _doubleArrow = ObjectExtensions.<RCServiceResponseTime>operator_doubleArrow(rCServiceResponseTime, _function);
        result = _doubleArrow;
      }
      _xblockexpression = (result);
    }
    return _xblockexpression;
  }
  
  public RCEnterprise lookupCustomer(final String name) {
    RCEnterprise _xblockexpression = null;
    {
      IGenericQuery<RCEnterprise> query=getObjectFactory().createGenericQuery(RCEnterprise.class,"select e from com.shrct.businessprocess.organization.impl.RCEnterpriseImpl as e   where ((e.name) = (:name)) and  e.active = 1      ").setParameter("name",name);
      RCEnterprise result = query.uniqueResult();
      boolean _equals = Objects.equal(result, null);
      if (_equals) {RCEnterprise rCEnterprise=RCEnterpriseImpl.create(null,getObjectFactory());
        final Procedure1<RCEnterprise> _function = new Procedure1<RCEnterprise>() {
            public void apply(final RCEnterprise it) {
              Long _id = it.getId();
              String _plus = ("_" + _id);
              it.setCode(_plus);
              it.setName(name);
            }
          };
        RCEnterprise _doubleArrow = ObjectExtensions.<RCEnterprise>operator_doubleArrow(rCEnterprise, _function);
        result = _doubleArrow;
      }
      _xblockexpression = (result);
    }
    return _xblockexpression;
  }
  
  public RCSupplier lookupSupplier(final String name) {
    RCSupplier _xblockexpression = null;
    {
      IGenericQuery<RCSupplier> query=getObjectFactory().createGenericQuery(RCSupplier.class,"select e from com.shrct.businessprocess.organization.impl.RCSupplierImpl as e   where ((e.name) = (:name)) and  e.active = 1      ").setParameter("name",name);
      RCSupplier result = query.uniqueResult();
      boolean _equals = Objects.equal(result, null);
      if (_equals) {RCSupplier rCSupplier=RCSupplierImpl.create(getObjectFactory());
        final Procedure1<RCSupplier> _function = new Procedure1<RCSupplier>() {
            public void apply(final RCSupplier it) {
              Long _id = it.getId();
              String _plus = ("_" + _id);
              it.setCode(_plus);
              it.setName(name);
            }
          };
        RCSupplier _doubleArrow = ObjectExtensions.<RCSupplier>operator_doubleArrow(rCSupplier, _function);
        result = _doubleArrow;
      }
      _xblockexpression = (result);
    }
    return _xblockexpression;
  }
  
  public RCBrand lookupBrand(final String name) {
    RCBrand _xblockexpression = null;
    {
      IGenericQuery<RCBrand> query=getObjectFactory().createGenericQuery(RCBrand.class,"select e from com.shrct.businessprocess.product.impl.RCBrandImpl as e   where ((e.name) = (:name)) and  e.active = 1      ").setParameter("name",name);
      RCBrand result = query.uniqueResult();
      boolean _equals = Objects.equal(result, null);
      if (_equals) {RCBrand rCBrand=RCBrandImpl.create(getObjectFactory());
        final Procedure1<RCBrand> _function = new Procedure1<RCBrand>() {
            public void apply(final RCBrand it) {
              Long _id = it.getId();
              String _plus = ("_" + _id);
              it.setCodeid(_plus);
              it.setName(name);
            }
          };
        RCBrand _doubleArrow = ObjectExtensions.<RCBrand>operator_doubleArrow(rCBrand, _function);
        result = _doubleArrow;
      }
      _xblockexpression = (result);
    }
    return _xblockexpression;
  }
  
  public RCProjectMock lookupServiceContract(final String contractId, final RCEnterprise customer) {
    RCProjectMock _xblockexpression = null;
    {
      IGenericQuery<RCProjectMock> query=getObjectFactory().createGenericQuery(RCProjectMock.class,"select e from com.shrct.businessprocess.customerservice.impl.RCProjectMockImpl as e   where ((e.contractId) = (:contractId)) and  e.active = 1      ").setParameter("contractId",contractId);
      RCProjectMock result = query.uniqueResult();
      boolean _equals = Objects.equal(result, null);
      if (_equals) {
        final Procedure1<RCProjectMock> _function = new Procedure1<RCProjectMock>() {
            public void apply(final RCProjectMock it) {
              it.setProjectName(contractId);
            }
          };
        RCProjectMock _createAsMockProject = this.serviceContractDelegateDAO.createAsMockProject(contractId, contractId, customer, _function);
        result = _createAsMockProject;
      }
      _xblockexpression = (result);
    }
    return _xblockexpression;
  }
  
  public RCProductSpecification lookupProductSpec(final String code) {
    RCProductSpecification _xblockexpression = null;
    {
      IGenericQuery<RCProductSpecification> query=getObjectFactory().createGenericQuery(RCProductSpecification.class,"select e from com.shrct.businessprocess.product.impl.RCProductSpecificationImpl as e   where ((e.code) = (:code)) and  e.active = 1      ").setParameter("code",code);
      RCProductSpecification result = query.uniqueResult();
      boolean _equals = Objects.equal(result, null);
      if (_equals) {
        final Procedure1<RCProductSpecification> _function = new Procedure1<RCProductSpecification>() {
            public void apply(final RCProductSpecification it) {
              it.setProductMetaType(RCProductMetaType.identifiable);
            }
          };
        RCProductSpecification _create = this.productSpecificationDAO.create(code, _function);
        result = _create;
      }
      _xblockexpression = (result);
    }
    return _xblockexpression;
  }
  
  public RCIdentifiablePhysicalProductEntry findOrImportProduct(final String projectid, final RCEnterprise customer, final RCSupplier supplier, final RCProductSpecification productSpec, final RCProjectMock contract, final String sid, final String location) {
    RCIdentifiablePhysicalProductEntry _xblockexpression = null;
    {
      final RCIdentifiablePhysicalProductEntry result = this.productService.findProductBySid(sid);
      boolean _notEquals = (!Objects.equal(result, null));
      if (_notEquals) {
        return result;
      }
      RCIdentifiablePhysicalProductEntry _importPhysicalProduct = this.productService.importPhysicalProduct(projectid, customer, sid);
      final Procedure1<RCIdentifiablePhysicalProductEntry> _function = new Procedure1<RCIdentifiablePhysicalProductEntry>() {
          public void apply(final RCIdentifiablePhysicalProductEntry it) {
            it.setType(productSpec);
            it.setSupplier(supplier);
            it.setLocation(location);
            RCLogicProduct _logicProduct = it.getLogicProduct();
            contract.addtoShippingList(_logicProduct);
          }
        };
      RCIdentifiablePhysicalProductEntry _doubleArrow = ObjectExtensions.<RCIdentifiablePhysicalProductEntry>operator_doubleArrow(_importPhysicalProduct, _function);
      _xblockexpression = (_doubleArrow);
    }
    return _xblockexpression;
  }
  
  public RCIdentifiablePhysicalProductEntry importProduct(final String projectid, final RCEnterprise customer, final RCSupplier supplier, final RCProductSpecification productSpec, final RCProjectMock contract, final String sid, final String location) {
    RCIdentifiablePhysicalProductEntry _importPhysicalProduct = this.productService.importPhysicalProduct(projectid, customer, sid);
    final Procedure1<RCIdentifiablePhysicalProductEntry> _function = new Procedure1<RCIdentifiablePhysicalProductEntry>() {
        public void apply(final RCIdentifiablePhysicalProductEntry it) {
          it.setType(productSpec);
          it.setSupplier(supplier);
          it.setLocation(location);
          RCLogicProduct _logicProduct = it.getLogicProduct();
          contract.addtoShippingList(_logicProduct);
        }
      };
    RCIdentifiablePhysicalProductEntry _doubleArrow = ObjectExtensions.<RCIdentifiablePhysicalProductEntry>operator_doubleArrow(_importPhysicalProduct, _function);
    return _doubleArrow;
  }
  
  public void importDefaultProductService(final String projectid, final String serviceName, final String serviceContractCode, final RCSupplier productSupplier, final RCIdentifiablePhysicalProductEntry product, final RCServiceLevel serviceLevel, final RCServiceResponseTime serviceResponseTime, final Integer months, final Date dueDate) {
    RCProductSpecification _type = product.getType();
    String _nullsafe = edu.fudan.mylang.common.StringExtensions.nullsafe(serviceName);
    final RCDerivedFullServiceProductSpecification s = this.productSpecificationDAO.getFullService(_type, _nullsafe, serviceLevel, serviceResponseTime, months);
    RCLogicProduct _logicProduct = product.getLogicProduct();
    String _nullsafe_1 = edu.fudan.mylang.common.StringExtensions.nullsafe(serviceName);
    int _minus = (-months);
    Date _addMonths = DateUtil.addMonths(dueDate, _minus);
    RCFullServiceProductEntry _registerFullServiceProduct = _logicProduct.registerFullServiceProduct(_nullsafe_1, projectid, serviceContractCode, s, _addMonths, dueDate);
    final Procedure1<RCFullServiceProductEntry> _function = new Procedure1<RCFullServiceProductEntry>() {
        public void apply(final RCFullServiceProductEntry it) {
          it.setSupplier(productSupplier);
        }
      };
    ObjectExtensions.<RCFullServiceProductEntry>operator_doubleArrow(_registerFullServiceProduct, _function);
  }
  
  public void importDefaultPromisedService(final String projectid, final String serviceName, final RCIdentifiablePhysicalProductEntry product, final RCServiceLevel serviceLevel, final RCServiceResponseTime serviceResponseTime, final Integer months, final Date dueDate) {
    RCLogicProduct _logicProduct = product.getLogicProduct();
    String _nullsafe = edu.fudan.mylang.common.StringExtensions.nullsafe(serviceName);
    int _minus = (-months);
    Date _addMonths = DateUtil.addMonths(dueDate, _minus);
    _logicProduct.registerCompanyService(projectid, _nullsafe, serviceLevel, serviceResponseTime, months, _addMonths, dueDate);
  }
}
